﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.IO;
using System.Xml.Linq;
using VideoClassLibrary.Model;
using VideoClassLibrary.BusinessLogicLayer;
public partial class CourseManagement_AddCourse : AdminUserBase
{
    protected void Page_Load(object sender, EventArgs e)
    {
            if (!IsPostBack)
            {
            TeacherModule ta = new TeacherModule();
            List<V_School> vs = new List<V_School>();
            vs = ta.GetSchool();
            dlschool.DataSource = vs;
            dlschool.DataBind();

            if (this.CurrentUser.SchoolID != 0)
            {
                dlschool.SelectedValue = this.CurrentUser.SchoolID.ToString();
                dlschool.Enabled = false;
            }
           
           }
    }
    protected void btnReg_Click(object sender, EventArgs e)
    {
        V_course cource = new V_course();
        CourseModule cm = new CourseModule();
        cource.TrainClassID = Convert.ToInt32(hidTrainClassIdList.Value);
        cource.Create_time = DateTime.Now;
        cource.SchoolID = Convert.ToInt32(dlschool.SelectedValue);
        cource.Update_time = DateTime.Now;
        cource.VC_desc = this.txtdesc.Text.ToString();
        cource.Vc_content = this.txtcontent.Text.ToString();
        cource.Vc_targetUsers = this.txttagerUser.Text;
        cource.VC_name = txtCourseName.Text.Trim();
        cource.CourseImage = string.Empty;
        string fileExtension = string.Empty;
        if (!string.IsNullOrEmpty(this.fuNewsImage.FileName))//修改图片
        {
            fileExtension = System.IO.Path.GetExtension(this.fuNewsImage.FileName);
            if (fileExtension != "" && fileExtension.ToLower() != ".jpg" && fileExtension.ToLower() != ".gif")
            {
                PageHelper.AlterMsg("图片格式只支持jpg/gif");
                return;
            }
        }
        int result = cm.AddCourseModel(cource);
        if (result > 0)
        {           
            if (!string.IsNullOrEmpty(this.fuNewsImage.FileName))//修改图片
            {
                fileExtension = System.IO.Path.GetExtension(this.fuNewsImage.FileName);
                if (fileExtension != "" && fileExtension.ToLower() != ".jpg" && fileExtension.ToLower() != ".gif")
                {
                    PageHelper.AlterMsg("图片格式只支持jpg/gif");
                    return;
                }
                string dictionarySavedPath = PageHelper.GetWebConfigValue("SchoolImagePath");
                dictionarySavedPath += "\\Images\\SchoolId\\Course\\";
                dictionarySavedPath = dictionarySavedPath.Replace("SchoolId", this.SchoolID.ToString());
                if (!Directory.Exists(dictionarySavedPath))
                {
                    Directory.CreateDirectory(dictionarySavedPath);
                }
                // dictionarySavedPath = Server.MapPath(PageHelper.ApplicationPath) + PageHelper.GetWebConfigValue("VideoPath").Replace("SchoolId", currentSchoolId.ToString());   
                //保存logo的文件夹绝对路径

                string logfileSavedPath = dictionarySavedPath +  result.ToString() + fileExtension;
                //fileSavedPath = dictionarySavedPath + "/logo" + fileExtension;
                this.fuNewsImage.SaveAs(logfileSavedPath);

                cource.CourseImage = ("/Images/SchoolId/Course/" +  result.ToString() + fileExtension).Replace("SchoolId", this.SchoolID.ToString());

                cm.UpdateCoursel(result, cource.CourseImage);
            }
        }
        if (result > 0)
        {
            PageHelper.AlterMsgAndRedirect("添加课程成功！", "SearchCourseList.aspx");
        }
        else
        {
            Label1.Text = "添加课程失败！";
        }
    }

    protected void Button1_Click(object sender, EventArgs e)
    {

        Response.Redirect("SearchCourseList.aspx");
    }
}
